package com.mzxy.mztopline;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.mzxy.mztopline.adapter.MyFragmentPargerAdapter;
import com.mzxy.mztopline.framgent.CountFragment;
import com.mzxy.mztopline.framgent.HomeFragment;
import com.mzxy.mztopline.framgent.MeFragment;
import com.mzxy.mztopline.framgent.VideoFragment;

import java.util.ArrayList;
import java.util.List;

import static com.mzxy.mztopline.R.layout.activity_main;

public class MainActivity extends AppCompatActivity {

     private   TextView tvMainTitle;
     private RadioGroup radioGroup;
     private ViewPager  viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(activity_main);

        tvMainTitle  = findViewById(R.id.tv_main_title);
        tvMainTitle.setText("首页");

        radioGroup =  findViewById(R.id.radioGroup);

        viewPager = findViewById(R.id.viewPager);
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new CountFragment());
        fragments.add(new VideoFragment());
        fragments.add(new MeFragment());

        FragmentPagerAdapter adapter = new MyFragmentPargerAdapter(getSupportFragmentManager(),fragments);


        viewPager.setAdapter(adapter);



        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_home:
                        //点击首页
                        Toast.makeText(MainActivity.this,"首页",Toast.LENGTH_SHORT).show();
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.rb_count:
                        //点击统计
                        Toast.makeText(MainActivity.this,"统计",Toast.LENGTH_SHORT).show();
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.rb_video:
                        //点击视频
                        Toast.makeText(MainActivity.this,"视频",Toast.LENGTH_SHORT).show();
                        viewPager.setCurrentItem(2);
                        break;
                    case R.id.rb_me:
                        //点击我
                        Toast.makeText(MainActivity.this,"我",Toast.LENGTH_SHORT).show();
                        viewPager.setCurrentItem(3);
                        break;
                }
            }
        });

        viewPager.setOffscreenPageLimit(3);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case  0 :
                        radioGroup.check(R.id.rb_home);
                        tvMainTitle.setText("首页");
                        break;
                    case  1 :
                        radioGroup.check(R.id.rb_count);
                        tvMainTitle.setText("统计");
                        break;
                    case  2 :
                        radioGroup.check(R.id.rb_video);
                        tvMainTitle.setText("视频");
                        break;
                    case  3 :
                        radioGroup.check(R.id.rb_me);
                        tvMainTitle.setText("我");
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });




    }



    long lastTime = 0 ;

}
